﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Crgd.UAuthServer.T4.Entities
{
	/// <summary>
	/// 梅举类型字符串：@E@SyncHandleStatus|UnHandle,0,待处理|Handling,1,处理中|Handled,2,已处理|Failed,3,处理失败|Canceled,90,已取消
	/// </summary>
	public sealed class EnumInfo
	{
		public EnumInfo() { }

		public EnumInfo(string enumTextInfo, string description)
		{
			if (string.IsNullOrEmpty(enumTextInfo))
			{
				throw new ArgumentNullException("enumTextInfo", "枚举定义文本不能为空！");
			}

			var tempTextInfo = enumTextInfo.Trim();
			if (!tempTextInfo.StartsWith("@@"))
			{
				throw new ArgumentException("enumTextInfo", "枚举定义文本格式不正确！");
			}


			var textInfos = enumTextInfo.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

			var enumNames = textInfos[0].Split(new string[] { @"@@" }, StringSplitOptions.RemoveEmptyEntries);
			this.EnumName = enumNames.FirstOrDefault();
			this.Values = new List<EnumValue>();

			var newDescription = new StringBuilder(enumNames.LastOrDefault());
			for (int i = 1; i < textInfos.Length; i++)
			{
				var enumValue = new EnumValue(textInfos[i]);
				this.Values.Add(enumValue);

                if (i == 1)
                {
					newDescription.Append("(");
				}

				newDescription.Append(enumValue.ToString());

				if (i == textInfos.Length - 1)
                {
					newDescription.Append(")");
                }
			}

			this.EnumDescription = newDescription.ToString();
		}

		public string EnumName { get; set; }

		public string EnumDescription { get; set; }

		public IList<EnumValue> Values { get; set; }
	}
}
